//
//  LXCarSignListViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/6.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarSignListViewController.h"
#import "LXSignContractModel.h"
#import "LXCarSignListTableViewCell.h"
#import "LXTopSearchView.h"
#import "LXSignContractDetailViewController.h"
#import "LXCustomerDeatilViewController.h"
@interface LXCarSignListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic) UITableView *tableView;
@property(nonatomic) NSMutableArray *data;
@property (nonatomic) LXTopSearchView *searchView;
@property (nonatomic) NSString *searchStr;
@property (nonatomic) NSInteger page;

@end

@implementation LXCarSignListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
    
    [self layoutCustomNav:self.view title:@"签约记录"];

    self.data = [NSMutableArray array];
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0,kViewTopHeight, kScreenWidth, 70)];
    searchView.textField.placeholder = @"输入车牌/客户进行搜索";
    [self.view addSubview:searchView];
    self.searchView = searchView;
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        [self.view endEditing:YES];
        self.searchStr = text;
        [self.tableView.mj_header beginRefreshing];
        
    }];
    
    
    
    [self.view addSubview:self.tableView];
    
   
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.page++;
        [self loadData];
    }];
    
    [self loadData];
}

-(void)loadData{
    
    NSString *url = [DOMAIN_URL stringByAppendingFormat:SigncontractPage_URL];
    
    NSMutableDictionary *dic = @{
        @"page":@(self.page),
        @"rows":@(10),
    }.mutableCopy;
    
    if (!StringIsEmpty(self.carNum)) {
        [dic setValue:self.carNum forKey:@"carNum"];
    }
    else if(!StringIsEmpty(self.customerId)){
        [dic setValue:self.customerId forKey:@"customerId"];
    }
    
    if (!StringIsEmpty(self.searchStr)) {
        [dic setValue:self.searchStr forKey:@"searchKey"];
    }
    
    [MSNetworkManager getRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        
        [self endRefreshing];
        
        if (isSuccess) {
            
            if (self.page==1) {
                [self.data removeAllObjects];
                [self.tableView.mj_footer resetNoMoreData];
            }
            NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
            NSArray *records = resonseObj[@"records"];
            
            [self.data addObjectsFromArray:[LXSignContractModel  mj_objectArrayWithKeyValuesArray:records]];
            
            
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
          
            
            
        }
        
        [self.tableView reloadData];
        
        [self isHaveEmptyData];
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LXCarSignListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXCarSignListTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    LXSignContractModel *model = self.data[indexPath.row];
    cell.model = model;
    // 客户
    [cell.btn1 whenTapped:^{
       
        LXCustomerDeatilViewController *vc = [[LXCustomerDeatilViewController alloc] init];
        vc.ID = model.customerId;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    // 电话
    [cell.btn2 whenTapped:^{
        [NSString callPhone:model.driverPhone];
    }];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    
    LXSignContractModel *model = self.data[indexPath.row];

    if (self.toSendValue) {
        self.toSendValue(model);
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    LXSignContractDetailViewController *vc = [[LXSignContractDetailViewController alloc] init];
    vc.signContractModel = model;
    [self pushToNextController:vc];
    

}


- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        [self.tableView.mj_footer resetNoMoreData];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchView.Y_height, ScreenWidth, kScreenHeight - self.searchView.Y_height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight = 196;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXCarSignListTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXCarSignListTableViewCell class])];
    }
    return _tableView;
}
@end
